local shenya = fk.CreateSkill {
  name = "ls__shenya",
}

Fk:loadTranslationTable{
  ["ls__shenya"] = "神鸦",
  [":ls__shenya"] = "一轮开始时，你可任意名角色于此轮获得“支援”；一轮结束后，若你于此轮未发动“支援”，你对此轮发动过“支援”的这些角色各造成1点伤害；否则，你视为对此轮未发动“支援”的这些角色使用<a href=':looting'>【趁火打劫】</a>。",
  ["#ls__shenya-target"] = "神鸦：你可令任意名角色获得“支援”",
  ["@@shenya-round"] = "神鸦：已支援",

  ["$ls__shenya1"] = "此次进击，定要丧敌胆魄。",
  ["$ls__shenya2"] = "战胜群敌，展江东豪杰之魄。",
}


shenya:addEffect(fk.RoundStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shenya.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    --local n = math.floor(#room.alive_players / 2)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = #room.alive_players,
      targets = room:getAlivePlayers(),
      skill_name = shenya.name,
      prompt = "#ls__shenya-target",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    for _, p in ipairs(tos) do
      room:addTableMarkIfNeed(player, "shenya-round", p.id)
      if not p:hasSkill("lvshi__zhiyuan&") then
        room:handleAddLoseSkills(p, "lvshi__zhiyuan&")
        room.logic:getCurrentEvent():findParent(GameEvent.Round, true, Player.HistoryGame):addCleaner(function ()
          room:handleAddLoseSkills(p, "-lvshi__zhiyuan&")
        end)
      end
    end
  end,
})


shenya:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(shenya.name) then
      for _, move in ipairs(data) do
        if move.skillName == "lvshi__zhiyuan&" then
          return table.contains(player:getTableMark("shenya-round"), move.from.id)
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      player.room:addPlayerMark(move.from, "@@shenya-round")
    end
  end,
})

shenya:addEffect(fk.RoundEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shenya.name)
  end,
  on_use = function(self, event, target, player, data)
    local tos = table.map(player:getTableMark("shenya-round"), Util.Id2PlayerMapper)
    if player:getMark("@@shenya-round") == 0 then
      for _, p in ipairs(tos) do
        if p:getMark("@@shenya-round") > 0 then
          player.room:damage({
            from = player,
            to = p,
            damage = 1,
            damageType = fk.NormalDamage,
            skillName = shenya.name,
          })
        end
      end
    else
      local targets = table.filter(tos, function (p) return p:getMark("@@shenya-round") == 0 end)
      if #targets > 0 then
        player.room:useVirtualCard("looting", nil, player, targets, shenya.name, true)
      end
    end
  end,
})

return shenya